package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.myapplication.bean.UserBean;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

public class Register extends AppCompatActivity {
    EditText mEtUserName,mEtPassWord,mEtRePassWord;
    Button mBtnRegister;
    private static final String TAG = "MainActivity";
    String data;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initEvent();
    }


    private void initEvent() {
        mBtnRegister.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                final String username =mEtUserName.getText().toString().trim();
                String password = mEtPassWord.getText().toString().trim();
                String repassword = mEtRePassWord.getText().toString().trim();

                String url ="http://anli-wechat.boruiman.com/api/UserServer/user_register";

                JSONObject jsonObject =new JSONObject();
                try {
                    jsonObject.put("username",username);
                    jsonObject.put("password",password);
                    jsonObject.put("repassword",repassword);
                }catch (JSONException e){
                    e.printStackTrace();
                }
                RequestQueue requestQueue = Volley.newRequestQueue(Register.this);
                JsonObjectRequest jsonObjectRequest =new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
//                        Gson gson =new Gson();
//
//                        UserBean userBean = gson.fromJson(jsonObject.toString(),UserBean.class);
                        Toast.makeText(Register.this, ""+jsonObject.optString("msg"), Toast.LENGTH_SHORT).show();
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Log.i(TAG, "onErrorResponse: "+ volleyError.getMessage().toString());
                        Toast.makeText(Register.this, "网络请求失败", Toast.LENGTH_SHORT).show();
                    }
                });
                requestQueue.add(jsonObjectRequest);
            }
        });
    }

    private void initView() {

        mEtUserName = findViewById(R.id.et_username);
        mEtPassWord = findViewById(R.id.et_password);
        mEtRePassWord = findViewById(R.id.et_repassword);
        mBtnRegister = findViewById(R.id.btn_register);
    }

}